package com.hanhai.zrb.backend.web.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;
import com.hanhai.common.annotation.PermissionNeed;
import com.hanhai.common.web.controller.BaseController;
import com.hanhai.zrb.backend.admin.model.Privilege;
import com.hanhai.zrb.backend.admin.service.PrivilegeService;

@Controller
@RequestMapping("privilege")
public class PrivilegeController extends BaseController {
	
	@Resource
	private PrivilegeService privilegeService;

	@PermissionNeed("privilege_privilege_list")
	@RequestMapping("")
	public String index() {
		return "privilege/list";
	}

	@PermissionNeed("privilege_privilege_list")
	@RequestMapping("list")
	public void list(HttpServletResponse response) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Map<String, Object>> list = privilegeService.listAll();
		int total = list.size();
		map.put("rows", list);
		map.put("total", total);
		String str = JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}
	

	@PermissionNeed("privilege_privilege_management")
	@RequestMapping("doadd")
	public void doadd(Privilege params, Model model,
			HttpServletRequest request, HttpServletResponse response) {
		privilegeService.add(params);
		super.jsonSucceed(response);
	}

	
	@PermissionNeed("privilege_privilege_management")
	@RequestMapping("add")
	public String add(Model model) {
		List<Map<String, Object>> privilegeList = privilegeService.listAll();
		model.addAttribute("privilegeList", privilegeList);
		return "privilege/add";
	}

	@PermissionNeed("privilege_privilege_management")
	@RequestMapping("edit")
	public String edit(@RequestParam Integer acl, Model model) {
		Map<String, Object> privilege = privilegeService.getDetail(acl);
		List<Map<String, Object>> privilegeList = privilegeService.listAll();
		model.addAttribute("privilegeList", privilegeList);
		model.addAttribute("privilege", privilege);
		return "privilege/edit";
	}

	@PermissionNeed("privilege_privilege_management")
	@RequestMapping("doupdate")
	public void doupdate(Privilege params, Model model,
			HttpServletRequest request, HttpServletResponse response) {
		privilegeService.update(params);
		super.jsonSucceed(response);
	}

	

	@PermissionNeed("privilege_privilege_management")
	@RequestMapping("delete")
	public void delete(Integer acl, Model model, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		boolean succeed = privilegeService.delete(acl);
		Map<String, Object> map=new HashMap<String, Object>();
		if(succeed){
			map.put("success", true);
		}else{
			map.put("success", false);
			map.put("infoMsg", "该角色下存在用户");
		}
		String str=JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}

	
}
